# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ingo Maindorfer <ingo@liquidcooling.de>
# Based on work by AlexWMF <alexxwmf@gmail.com>

_realname=qwt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
         $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6"))
pkgver=6.2.0
pkgrel=8
pkgdesc="Qt Widgets for Technical Applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=("custom:${_realname}")
url="https://qwt.sourceforge.io/"
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-qt5-svg"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             $([[ ${CARCH} == i686 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-qt6-base" \
               "${MINGW_PACKAGE_PREFIX}-qt6-svg" \
               "${MINGW_PACKAGE_PREFIX}-qt6-tools"))
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.bz2"
        001-release-build.patch
        002-link-to-release.patch
        003-qwtconfig-msys2.patch
        004-pkgconfig-file.patch
        005-rename-qwt-to-remove-qt5-qt6-conflict.patch)
sha256sums=('9194f6513955d0fd7300f67158175064460197abab1a92fa127a67a4b0b71530'
            '582b8e203243676e92fbebf4e8d078f5adcbe077834976beff00cea53179c74d'
            '6e0101d2c2897c94da6284ba0b929a7a39d82120313e6cd6353fd5e3aaf4b059'
            '92c49c72bc4fc241157dd9662082b0d2bf35f7bdb7de2778632a82f498ee3871'
            'ce0dc70355b247a7c8e1ae4154cb2e8d8a4b683542e0322d86528cfa8264b651'
            '1c8628fb33e151b6aa33e10db76d81f740ea2a65e75c2bc7c6bd287e996bd77f')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    001-release-build.patch \
    002-link-to-release.patch \
    003-qwtconfig-msys2.patch \
    004-pkgconfig-file.patch \
    005-rename-qwt-to-remove-qt5-qt6-conflict.patch
}

build() {
  # qwt-qt5
  cp -rf ${srcdir}/${_realname}-${pkgver} ${srcdir}/build-${MSYSTEM}-qt5
  cd ${srcdir}/build-${MSYSTEM}-qt5

  local PREFIX_WIN_QT5=$(cygpath -am ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qwt-qt5${MINGW_PREFIX})
  sed -s "s|%VALUE_QWT_INSTALL_PREFIX%|${PREFIX_WIN_QT5}|g" -i qwtconfig.pri
  ${MINGW_PREFIX}/bin/qmake CONFIG-=debug_and_release CONFIG+=release qwt.pro

  make

  # qwt-qt6
  if [[ ${CARCH} != i686 ]]; then

  cp -rf ${srcdir}/${_realname}-${pkgver} ${srcdir}/build-${MSYSTEM}-qt6
  cd ${srcdir}/build-${MSYSTEM}-qt6

  local PREFIX_WIN_QT6=$(cygpath -am ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qwt-qt6${MINGW_PREFIX})
  sed -s "s|%VALUE_QWT_INSTALL_PREFIX%|${PREFIX_WIN_QT6}|g" -i qwtconfig.pri
  ${MINGW_PREFIX}/bin/qmake6 CONFIG-=debug_and_release CONFIG+=release qwt.pro

  make

  fi
}

package_qwt-qt5() {
  pkgdesc="Qt Widgets for Technical Applications - Qt5 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base"
           "${MINGW_PACKAGE_PREFIX}-qt5-svg")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-tools")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd ${srcdir}/build-${MSYSTEM}-qt5

  make -j1 install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt5/COPYING

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/

  local PREFIX_WIN=$(cygpath -am ${pkgdir}${MINGW_PREFIX})
  local PREFIX=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/share/qt5/mkspecs/features/qwtconfig.pri
  sed -s "s|qwtAddLibrary(\$\${QWT_INSTALL_LIBS}, qwt)|qwtAddLibrary(\$\${QWT_INSTALL_LIBS}, qwt-qt5)|g" \
    -i ${pkgdir}${MINGW_PREFIX}/share/qt5/mkspecs/features/qwt.prf
  sed -s "s|${PREFIX_WIN}|\$\{prefix\}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/Qt5Qwt6.pc
  sed -s "s|${PREFIX}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/Qt5Qwt6.pc
}

package_qwt-qt6() {
  pkgdesc="Qt Widgets for Technical Applications - Qt6 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-qt6-svg")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-tools")

  cd ${srcdir}/build-${MSYSTEM}-qt6

  make -j1 install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt6/COPYING

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/

  local PREFIX_WIN=$(cygpath -am ${pkgdir}${MINGW_PREFIX})
  local PREFIX=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/share/qt6/mkspecs/features/qwtconfig.pri
  sed -s "s|qwtAddLibrary(\$\${QWT_INSTALL_LIBS}, qwt)|qwtAddLibrary(\$\${QWT_INSTALL_LIBS}, qwt-qt6)|g" \
    -i ${pkgdir}${MINGW_PREFIX}/share/qt6/mkspecs/features/qwt.prf
  sed -s "s|${PREFIX_WIN}|\$\{prefix\}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/Qt6Qwt6.pc
  sed -s "s|${PREFIX}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/Qt6Qwt6.pc
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
